package com.snow.vpnclient.config;

import android.content.Context;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.NonNull;

/**
 * @author liufq
 * @since 2023-02-02 23:04:36
 */
public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private final Context context;

    public CrashHandler(Context context) {
        this.context = context;
//        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    /**
     * 重启应用
     */
    private void restartApp() {
//        Intent intent = new Intent(context, MainActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        context.startActivity(intent);
//        android.os.Process.killProcess(android.os.Process.myPid());//再此之前可以做些退出等操作
    }

    @Override
    public void uncaughtException(@NonNull Thread thread, @NonNull Throwable e) {
//        final Dialog dialog=new Dialog(context);
//        dialog.setContentView(R.layout.dialog_layout);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(context, "操作失败，请重启应用，" + e.getMessage(), Toast.LENGTH_LONG).show();
                Looper.loop();
            }
        }).start();

        try {
            thread.sleep(2000);
        } catch (InterruptedException e2) {
            e2.printStackTrace();
        }
        restartApp();

//        final Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        context. startActivity(intent);
//
//        //杀掉以前进程
//        android.os.Process.killProcess(android.os.Process.myPid());

//        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        context.startActivity(intent);

//        android.os.Process.killProcess(android.os.Process.myPid());

//        activity.runOnUiThread(() -> {
//            Toast.makeText(activity, "操作失败，" + e.getMessage(), Toast.LENGTH_SHORT).show();

//        Intent intent1 = new Intent(activity, MainActivity.class);
//        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//
//        Intent intent = activity.getPackageManager()
//                    .getLaunchIntentForPackage(activity.getPackageName());
//            PendingIntent restartIntent = PendingIntent.getActivity(activity, 0, intent, 0);
//            AlarmManager mgr = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
//            // 1秒钟后重启应用
//            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent);
//            System.exit(0);
//        });


//        ToastUtils.showShort("操作失败");
//        activity.runOnUiThread(() -> ToastUtils.showShort("操作失败"));
//        new Thread(() -> {
//
//
////            activity.runOnUiThread(() -> ToastUtils.showShort("操作失败"));
////            Looper.prepare();
////            activity.runOnUiThread(() -> ToastUtils.showShort("操作失败"));
////            Looper.loop();
//
////            SystemClock.sleep(3000);
////
////            // 将Activity的栈清空
////            AppManager.removeAll();
////            // 退出程序
////            Process.killProcess(Process.myPid());
////            // 关闭虚拟机，彻底释放内存空间
////            System.exit(0);
//
//        }).start();
    }
}
